package com.baosight.sczx.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.acegisecurity.Authentication;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.security.AuthenticationPostHandler;
import com.baosight.sczx.zm.domain.ZM03;

public class ZnhPostHandler implements AuthenticationPostHandler
{

	public String getDescription()
	{
		return null;
	}

	public void handle(HttpServletRequest request, HttpServletResponse response, Authentication authen, boolean success ) 
	{
		if( success )
		{
			HttpSession session = request.getSession();

			String user = UserSession.getLoginName();

			if (user == null) {
				throw new IllegalStateException( "ePass token NOT found");
			}
			
			ZM03 temp = getUserOrg(user);
			String orgCode = temp.getOrgCode();
			session.setAttribute("orgCode", orgCode);
			session.setAttribute("orgName", temp.getOrgName());
			String miningCode = "C." + orgCode.trim().substring(2, 7);
			session.setAttribute("miningCode", miningCode);
			session.setAttribute("miningName", getMiningName(miningCode));
			session.setAttribute("UserCname", temp.getEmpName());
		}
	}
	
	/**
	 * 
	 * <p>Function: getUserOrg</p>
	 * <p>Description: </p>
	 * <p>return value:Tes01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-12上午02:35:21 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private ZM03 getUserOrg(String uid) {
		Dao dao = (Dao) SpringApplicationContext.getBean("dao");
		Map map = new HashMap();
		map.put("name", uid);
		List list = dao.query("ZM03.query", map);
		if (list.size() <= 0) {
			return new ZM03();
		}
		ZM03 result = (ZM03)list.get(0);
		return result;
	}

	/**
	 * 
	 * <p>Function: getMiningName</p>
	 * <p>Description: </p>
	 * <p>return value:Tes01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年10月27日16:25:03 王伟
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private String getMiningName(String miningCode) {
		Dao dao = (Dao) SpringApplicationContext.getBean("dao");
		String miningName = "";
		Map map = new HashMap();
		map.put("orgCode", miningCode);
		List list = dao.query("ZM03.queryMiningName", map);
		if (list.size() <= 0) {
			return "";
		}
		HashMap result = (HashMap)list.get(0);
		if(result!=null)
			miningName = (String)result.get("orgName");	
		return miningName;
	}
	
}
